<CFQUERY NAME="qryCINLEAVEAPPSM" DATASOURCE="#client.company_dsn#">
	SELECT *
	FROM CINLEAVEAPPSM
	WHERE PERSONNELIDNO = '#GETENTRY.PERSONNELIDNO#' AND DOCNUMBER = '#GETENTRY.DOCNUMBER#'
</CFQUERY>
<CFIF qryCINLEAVEAPPSM.APPROVED NEQ "S" AND qryCINLEAVEAPPSM.APPROVED NEQ "Y">
	<CFSET theDate= DATEFORMAT("#qryCINLEAVEAPPSM.STARTINGDATE#")>
	<CFSET sNoofDays= DateDiff("d", "#qryCINLEAVEAPPSM.STARTINGDATE#", "#qryCINLEAVEAPPSM.ENDINGDATE#") + 1>
	<CFQUERY NAME="cleanCINLEAVEAPPSD" DATASOURCE="#client.company_dsn#">
		DELETE FROM CINLEAVEAPPSD
		WHERE DOCNUMBER  = '#qryCINLEAVEAPPSM.DOCNUMBER#'
	</CFQUERY>
	<CFOUTPUT QUERY="qryCINLEAVEAPPSM">
		<CFLOOP index="noofdays" FROM="1" TO="#sNoofDays#" STEP="1">
			<CFSET ValidateDate=DayOfWeekAsString(DayOfWeek("#theDate#"))>
			<CFIF ValidateDate NEQ "Sunday">
				<CFIF ValidateDate NEQ "Saturday">
					<CFIF qryCINLEAVEAPPSM.STAFFPID NEQ "">
						<CFQUERY NAME="insertCINLEAVEAPPSD" DATASOURCE="#client.company_dsn#">
							INSERT INTO CINLEAVEAPPSD(PERSONNELIDNO,REFERENCEDATE,LEAVEHOURS,LEAVETYPE,RECCREATEDBY,RECDATECREATED,USERID,DATELASTUPDATE,TIMELASTUPDATE,NOOFDAYS,DOCNUMBER,APPROVED,WITHPAY)
							VALUES('#STAFFPID#','#theDate#','#LEAVEHOURS#','#LEAVETYPE#','#RECCREATEDBY#','#RECDATECREATED#','#USERID#','#DATELASTUPDATE#','#TIMELASTUPDATE#','#NOOFDAYS#','#DOCNUMBER#','N','#WITHPAY#')
						</CFQUERY>
					<CFELSE>
						<CFQUERY NAME="insertCINLEAVEAPPSD" DATASOURCE="#client.company_dsn#">
							INSERT INTO CINLEAVEAPPSD(PERSONNELIDNO,REFERENCEDATE,LEAVEHOURS,LEAVETYPE,RECCREATEDBY,RECDATECREATED,USERID,DATELASTUPDATE,TIMELASTUPDATE,NOOFDAYS,DOCNUMBER,APPROVED,WITHPAY)
							VALUES('#PERSONNELIDNO#','#theDate#','#LEAVEHOURS#','#LEAVETYPE#','#RECCREATEDBY#','#RECDATECREATED#','#USERID#','#DATELASTUPDATE#','#TIMELASTUPDATE#','#NOOFDAYS#','#DOCNUMBER#','N','#WITHPAY#')
						</CFQUERY>
					</CFIF>
				</CFIF>
			</CFIF>
			<CFSET ValidateDate="">
			<CFSET theDate= DATEFORMAT(DateAdd("D",1, "#theDate#"))>
		</CFLOOP>
	</cfoutput>
</CFIF>
